package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public BusinessDataVO getBusinessData() {
        LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        //获取当天所有订单
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("order_time", start, end);
        List<Orders> ordersList = orderMapper.selectList(queryWrapper);
        //计算营业额
        double totalAmount = ordersList.stream()
                .filter(o -> o.getStatus() == 5)   // 筛选出状态为5的订单
                .map(Orders::getAmount)            // 映射到BigDecimal型的金额，假设Order类有getAmount方法
                .map(BigDecimal::doubleValue)      // 将每个BigDecimal转换为double类型
                .reduce(0.0, Double::sum);  // 计算所有金额的总和

        //统计有效订单数
        Integer validOrders = Math.toIntExact(ordersList.stream().filter(o -> o.getStatus() == 5)
                .count());

        //平均客单价
        double avgCustomerPrice = totalAmount / validOrders;


        return BusinessDataVO.builder()
                .newUsers(1)
                .turnover(totalAmount)
                .validOrderCount(validOrders)
                .orderCompletionRate(1.0)
                .unitPrice(avgCustomerPrice)
                .build();

    }

    @Override
    public SetmealOverViewVO getSetmealOverViewVO() {
        List<Setmeal> EnableStatus = setmealMapper.selectList(new QueryWrapper<Setmeal>().eq("status", 1));
        List<Setmeal> loseStatus = setmealMapper.selectList(new QueryWrapper<Setmeal>().eq("status", 0));

        return SetmealOverViewVO.builder()
                .sold(EnableStatus.size())
                .discontinued(loseStatus.size())
                .build();
    }

    @Override
    public DishOverViewVO getDishOverViewVO() {
        List<Dish> EnableStatus = dishMapper.selectList(new QueryWrapper<Dish>().eq("status", 1));
        List<Dish> loseStatus = dishMapper.selectList(new QueryWrapper<Dish>().eq("status", 0));

        return DishOverViewVO.builder()
                .sold(EnableStatus.size())
                .discontinued(loseStatus.size())
                .build();
    }

    @Override
    public OrderOverViewVO getOrderOverViewVO() {
        LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        //获取当天所有订单
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("order_time", start, end);
        List<Orders> ordersList = orderMapper.selectList(queryWrapper);
        //统计待接单数量
        Integer pendingOrderCount =Math.toIntExact(ordersList.stream().filter(o -> o.getStatus() == 2).count());
        //统计待派送数量
        Integer acceptedOrderCount =Math.toIntExact(ordersList.stream().filter(o -> o.getStatus() == 3).count());
        //统计已完成
        Integer completedOrderCount =Math.toIntExact(ordersList.stream().filter(o -> o.getStatus() == 5).count());
        //统计已取消
        Integer canceledOrderCount =Math.toIntExact(ordersList.stream().filter(o -> o.getStatus() == 6).count());

        return OrderOverViewVO.builder()
                .allOrders(ordersList.size())
                .completedOrders(completedOrderCount)
                .deliveredOrders(acceptedOrderCount)
                .waitingOrders(pendingOrderCount)
                .cancelledOrders(canceledOrderCount)
                .build();
    }

}
